package com.lawliet.example.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lawliet.example.model.system.SysPermission;
import com.lawliet.example.model.system.SysRole;
import com.lawliet.example.model.system.SysUser;
import com.lawliet.example.model.system.vo.PermissionTreeVO;
import com.lawliet.example.model.vo.Result;
import com.lawliet.example.service.system.SysRoleService;
import com.lawliet.example.util.QueryHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Tag(name = "SysRole 接口")
@RestController
@RequestMapping("/api/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Operation(summary = "角色列表")
    @GetMapping("/list")
    public Result<?> list(@Parameter(description = "查询条件") SysRole data,
                                      @Parameter(description = "当前页") @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                      @Parameter(description = "每页显示条数") @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                      HttpServletRequest req) {

        Page<SysRole> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SysRole> sysRoleQueryWrapper = QueryHelper.buildQueryWrapper(data);
        sysRoleQueryWrapper.eq("DEL_FLAG",0);//没有被删除的角色
        IPage<SysRole> pageList = sysRoleService.page(page, sysRoleQueryWrapper);

        return Result.OK(pageList);
    }

    @Operation(summary = "新增角色")
    @PostMapping("/add")
    public Result<?> add(@Parameter(description = "角色对象") @RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return Result.OK("添加成功");
    }

    @Operation(summary = "新增角色")
    @PutMapping("/edit")
    public Result<?> edit(@Parameter(description = "角色对象") @RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return Result.OK("编辑成功");
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/delete")
    public Result<?> delete(@Parameter(description = "角色id") @RequestParam String ids) {
        List<String> roleIds = Arrays.asList(ids.split(","));
        for(String roleId : roleIds){
            sysRoleService.removeById(Long.parseLong(roleId));
        }
        return Result.OK("删除成功");
    }

    @Operation(summary = "分配权限")
    @DeleteMapping("/assignPermission")
    public Result<?> assignPermission(@Parameter(description = "角色id") @RequestParam Long roleId,
                                      @Parameter(description = "权限id") @RequestParam String permissionIds) {
        sysRoleService.assignPermission(roleId,permissionIds);
        return Result.OK("分配成功");
    }


    @GetMapping("/query/{id}")
    public Result<SysRole> queryById(@PathVariable Long id) {
        return Result.OK(sysRoleService.getById(id));
    }

}
